/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wanhai.itm.wditm145.vm;

import com.wanhai.itm.wditm141.vm.*;
import com.wanhai.itm.wditm145.vo.WDITM145_ITM4011VO;
import com.wanhai.vo.itm.ITM3012VO;
import com.wanhai.vo.itm.ITM4005VO;
import com.wanhai.vo.itm.ITM4007VO;
import com.wanhai.vo.itm.ITM4008VO;
import com.wanhai.zkbase.core.WHL_BaseVM;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.zkoss.bind.annotation.AfterCompose;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.CheckEvent;
import org.zkoss.zk.ui.select.Selectors;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Include;
import org.zkoss.zul.Window;

/**
 *
 * @author m2434
 */
public class WDITM145_VM extends WHL_BaseVM {

    protected Log logger = LogFactory.getLog(getClass());
    private List<WDITM145_ITM4011VO> items = new ArrayList<WDITM145_ITM4011VO>();
    private WDITM145_ITM4011VO cond = new WDITM145_ITM4011VO();//query condition;
    private WDITM145_ITM4011VO selected;
    private String actionFlag;
    ///<editor-fold defaultstate="collapsed" desc="binding UI component">
    @Wire("#inc1")
    private Include inc1;
    @Wire("#inc2")
    private Include inc2;
    @Wire("#inc3")
    private Include inc3;
    //</editor-fold>

    @Init
    public void init() {
        WDITM145_ITM4011VO vo1 = new WDITM145_ITM4011VO();
        vo1.setWORK_DATE("20150401");
        vo1.setITM4006_SEQ_NO("PLAN ID 1");
        vo1.setITEM_NO("01");
        vo1.setHOUR_SPENT("1");
        vo1.setOBJECT_NAME("XXX");
        vo1.setDESCRPT("Description 01");

        WDITM145_ITM4011VO vo2 = new WDITM145_ITM4011VO();
        vo2.setWORK_DATE("20150401");
        vo2.setITM4006_SEQ_NO("PLAN ID 2");
        vo2.setITEM_NO("08");
        vo2.setHOUR_SPENT("2");
        vo2.setOBJECT_NAME("");
        vo2.setDESCRPT("Description 08Description 08Description 08Description 08Description 08Description 08Description 08Description 08Description 08Description 08Description 08Description 08Description 08Description 08Description 08Description 08Description 08Description 08Description 08Description 08Description 08Description 08Description 08Description 08Description 08Description 08Description 08Description 08Description 08Description 08Description 08Description 08Description 08Description 08Description 08Description 08Description 08");

        WDITM145_ITM4011VO vo3 = new WDITM145_ITM4011VO();
        vo3.setWORK_DATE("20150401");
        vo3.setITM4006_SEQ_NO("PLAN ID 3");
        vo3.setITEM_NO("34");
        vo3.setHOUR_SPENT("4");
        vo3.setOBJECT_NAME("");
        vo3.setDESCRPT("Leave");

        WDITM145_ITM4011VO vo4 = new WDITM145_ITM4011VO();
        vo4.setWORK_DATE("20150401");
        vo4.setITM4006_SEQ_NO("PLAN ID 4");
        vo4.setITEM_NO("36");
        vo4.setHOUR_SPENT("1");
        vo4.setOBJECT_NAME("XXX");
        vo4.setDESCRPT("Description 36");

        items.add(vo1);
        items.add(vo2);
        items.add(vo3);
        items.add(vo4);

    }

    @Command
    public void preInsert() {
        this.setActionFlag("insert");

        ((Window) inc1.getFellow("operatorWin")).doModal();
    }

    @Command
    public void preCheck() {
        this.setActionFlag("check");

        ((Window) inc2.getFellow("operatorWin")).doModal();
    }

    @Command
    public void preWeekPlan() {
        this.setActionFlag("weekPlan");

        ((Window) inc3.getFellow("operatorWin")).doModal();
    }

    @Command
    @NotifyChange("items")
    public void copy(@BindingParam("target") WDITM145_ITM4011VO target) {
        int selectedIndex = this.getItems().indexOf(target);
        WDITM145_ITM4011VO copyVo = null;

        try {
            copyVo = (WDITM145_ITM4011VO) deepCopy(target);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        this.getItems().add(selectedIndex, copyVo);
    }

    @AfterCompose
    public void afterCompose(@ContextParam(ContextType.VIEW) Component view) {
        Selectors.wireComponents(view, this, false);
    }

    @Command("triggerLOV")
    public void triggerLOV(@BindingParam("lovType") String type) {
        if ("ITEM_NO".equals(type)) {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("callbackCmd", "onLOVCallback");
            params.put("params", null);

            Window win = (Window) Executions.createComponents("~./wanhai/itm/wditm145/wditm145_lov_itemno.zul", rootView, params);

            win.doModal();
        }
    }

    @Command("onLOVCallback")
    @NotifyChange("selected")
    public void onLOVCallback(@BindingParam("callbackValue") ITM3012VO callbackObj) {
        if (this.selected != null) {
            this.selected.setITEM_NO(callbackObj.getITEMNO());
        }
    }

    /**
     * make a full copy of a VO
     *
     * @param oldObj
     * @return
     * @throws Exception
     */
    protected Object deepCopy(Object oldObj) throws Exception {
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(oldObj);
            oos.flush();
            ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray());
            ois = new ObjectInputStream(bin);
            return postCopyProcess(ois.readObject());
        } catch (Exception e) {
            throw e;
        } finally {
            oos.close();
            ois.close();
        }
    }

    /**
     * Manipulation VO after copy process
     *
     * @param m_VO
     * @return
     */
    protected Object postCopyProcess(Object m_VO) {
        HashMap<String, Object> defprops = new HashMap<String, Object>();
        defprops.put("row_id", null);
        defprops.put("modified", false);
//        defprops.put("identifyId", IDGenerator.createID());
        defprops.put("ENTRY_USER", null);
        defprops.put("ENTRY_OFFICE", null);
        defprops.put("ENTRY_DATE", null);
        defprops.put("MODIFY_USER", null);
        defprops.put("MODIFY_OFFICE", null);
        defprops.put("MODIFY_DATE", null);
        try {
            BeanUtils.populate(m_VO, defprops);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
        return m_VO;
    }

    //<editor-fold defaultstate="collapsed" desc="getter , setter">
    public List<WDITM145_ITM4011VO> getItems() {
        return items;
    }

    public void setItems(List<WDITM145_ITM4011VO> items) {
        this.items = items;
    }

    public String getActionFlag() {
        return actionFlag;
    }

    public void setActionFlag(String actionFlag) {
        this.actionFlag = actionFlag;
    }
    //</editor-fold>
}
